package com.forever7776.life.admin.controller.life.m;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.vo.FriendlyLinkSearchVO;
import com.forever7776.life.core.entity.FriendlyLink;
import com.forever7776.life.core.service.IFriendlyLinkService;
import enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

/**
 * <p>
 * 友情链接
 * </p>
 *
 * @author kz
 * @date 2018-03-27
 */
@Controller
@RequestMapping("/m/friendly/link")
public class ManageFriendlyLinkController extends BaseController {

    @Autowired
    private IFriendlyLinkService iFriendlyLinkService;

    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "friendly_link";
    }

    /**
     * 编辑链接页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public JSONObject getById(@PathVariable String id) {
        FriendlyLink link = iFriendlyLinkService.selectById(id);
        JSONObject result = ResultUtil.getSuccessResult("");
        result.put("link", link);
        return result;
    }


    /**
     * 添加链接
     *
     * @param link
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(FriendlyLink link) {
        JSONObject result = checkForm(link, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        iFriendlyLinkService.insert(link);
        return result;
    }

    /**
     * 编辑链接
     *
     * @param link
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateLink(@PathVariable String id, FriendlyLink link) {
        if (iFriendlyLinkService.selectById(id) == null) {
            return ResultUtil.getFailResult("该链接不存在！");
        }
        JSONObject result = checkForm(link, "编辑成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        iFriendlyLinkService.updateById(link);
        return result;
    }

    /**
     * 链接列表
     *
     * @param vo
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(FriendlyLinkSearchVO vo) {
        JSONObject result = new JSONObject();
        result.put("page", iFriendlyLinkService.selectAll(new Page<FriendlyLink>(pageNumber, pageSize), vo));
        return result;
    }

    /**
     * 修改友情链接状态
     *
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateLinkStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateLinkStatus(@PathVariable Integer id,
                                       @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (iFriendlyLinkService.selectById(id) == null) {
            return ResultUtil.getFailResult("该链接不存在！");
        }
        iFriendlyLinkService.updateLinkStatus(id, status);
        return ResultUtil.getSuccessResult("操作成功");
    }

    /**
     * 链接表单校验
     *
     * @param link
     * @param msg
     * @return
     */
    public JSONObject checkForm(FriendlyLink link, String msg) {
        if (link == null) {
            return ResultUtil.getFailResult("链接为空");
        }
        if (StringUtils.isEmpty(link.getCompany())) {
            return ResultUtil.getFailResult("公司名称为空");
        }
        if (StringUtils.isEmpty(link.getPic())) {
            return ResultUtil.getFailResult("logo为空");
        }
        if (StringUtils.isEmpty(link.getUrl())) {
            return ResultUtil.getFailResult("链接为空");
        }
        return ResultUtil.getSuccessResult(msg);
    }

}
